package worker

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
	"strconv"
	"strings"
	"time"
)

var netEasyCookie = http.Cookie{
	Name:  "_ntes_nuid",
	Value: "17427284b31845d17991b43e928857fd",
	// WM_TID=ltnsc54lVgBEBVRUQBYpuD7kZWU1PXjd; WM_NI=gDljpB6ird9iBJxZFFytHKGQCxDXxUxxrLIBGq0KGKEtQi2jYoWvtqPe5DAISSZK8E%2BGds3nrZIF%2ByFqgkZeqetL3Y8PECJgNc4owIS6KnzMqOFyurH7YZoP8iF0eW22RXg%3D; JSESSIONID-WYYY=oAXgO0bR7eo99Q%2B%5Cudn7PhuJqqMJrZFaecewG3TNgxVgPgivVed6PBmaR8YUNpf%5Clp%2FYQBxRMZ83AS%2B4pp7WfYnRQprwTjl2JV4NwyM%2BxNFT0Pr%2Bjv8Foc37Qfbzpw5juMb5VkSKMxWS%2F2chwuPbj0AwnEcP5ONxERfJuMFoYbT0dERR%3A1574044625865; WM_NIKE=9ca17ae2e6ffcda170e2e6eeaaf1449a9f86d5e545a19e8fa2d84b979b9ebabb3d95afa8aacb5bac958c8be72af0fea7c3b92aa59ca48bf34af6b7be85bc638296b8bbc54ebbac88acc65b81a88cb9fb5fbca8beadcc4a85af00acc64187ac9f8ab1629bb19997f77b94bbfba5c443918d96acbb4fe999ac94b266b28687a7aa7386afa98cd96fb3bbbcadb253fc908ba4c96d8ebdfc90f64391ea858ff947b3918a9af87a8ce997bbee5cfcb78ad1e85df2ee9eb9e637e2a3; _iuqxldmzr_=32; _ntes_nnid=17427284b31845d17991b43e928857fd,1574042825899; _ntes_nuid=17427284b31845d17991b43e928857fd
	Path:   "/",
	Domain: ".163.com",
	// 2039年10月3日星期一 下午8:49:12
	Expires: time.Date(2039, 10, 3, 8, 49, 12, 0, time.Local),
}

var netEasyTick = time.Tick(time.Second * 3)

func NetEasyMusicClient(songID string, limit, offset int64) ([]byte, error) {
	//songID := "28196001"
	<-netEasyTick

	fmt.Printf("[ NetEasyMusicClient ] song_id: %v\n", songID)
	csrf := ""
	var req = struct {
		CsrfToken string `json:"csrf_token"` // "csrf_token":"",
		Limit     string `json:"limit"`      // "limit":"20",
		Offset    string `json:"offset"`     // "offset":"0",
		Rid       string `json:"rid"`        // "rid":"R_SO_4_" + song_id,
		Total     string `json:"total"`      // "total":"true"
	}{
		CsrfToken: csrf,
		Limit:     strconv.Itoa(int(limit)),
		//Limit:     "20",
		Offset: strconv.Itoa(int(offset)),
		//Offset: "0",
		Rid:   "R_SO_4_" + songID,
		Total: "true",
	}
	bytes2, err := json.Marshal(req)
	if err != nil {
		return nil, err
	}
	//fmt.Println(string(bytes2))
	param, key, err := EncParams(string(bytes2))
	if err != nil {
		return nil, err
	}

	//fmt.Println("param:", param)
	//fmt.Println("key:", key)
	urlStr := "http://music.163.com/weapi/v1/resource/comments/R_SO_4_" + songID + "?csrf_token=" + csrf
	fmt.Println("url:", urlStr)
	params := url.Values{
		"params":    []string{param},
		"encSecKey": []string{key},
	}

	paramStr := params.Encode()
	fmt.Println("paramStr:", paramStr)

	request, err := http.NewRequest("POST", urlStr, strings.NewReader(paramStr))
	if err != nil {
		return nil, err
	}
	request.Header.Add("Host", "music.163.com")
	request.Header.Add("User-Agent", getUserAgent())
	request.Header.Add("Content-Type", "application/x-www-form-urlencoded")

	request.AddCookie(&netEasyCookie)

	//request.Header.Add("_ntes_nuid", "ff57c2c2391a39e121fc6db90f707bd7")

	resp, err := http.DefaultClient.Do(request)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()
	all, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	//t.Error(string(all))
	fmt.Println("all:", string(all))
	return all, nil
}
